package com.jiangxiaofei.citycloud.application;

import android.app.Application;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.SDKInitializer;
import com.jiangxiaofei.citycloud.constants.Constants;
import com.jiangxiaofei.citycloud.service.LocationService;
import com.jiangxiaofei.citycloud.util.L;
import com.jiangxiaofei.citycloud.util.LogUtil;
import com.jiangxiaofei.citycloud.util.MyLocationListener;
import com.jiangxiaofei.citycloud.util.SharedPreferencesUtil;
import com.jiangxiaofei.citycloud.util.T;
import com.jiangxiaofei.citycloud.util.Utils;
import com.taobao.sophix.SophixManager;
import com.taobao.sophix.listener.PatchLoadStatusListener;

/**
 * Created by 上官林超 on 2017/7/28.
 */

public class MainApplication extends Application {
    private static Context mContext;
    public LocationService locationService;
    private BDLocationListener myListener = new MyLocationListener();
    public LocationClient mLocationClient = null;

    public static synchronized Context getContext() {
        return mContext;
    }

    public interface MsgDisplayListener {
        void handle(String msg);
    }

    public static MsgDisplayListener msgDisplayListener = null;
    public static StringBuilder cacheMsg = new StringBuilder();

    private void initHotfix() {
        String appVersion;
        try {
            appVersion = this.getPackageManager().getPackageInfo(this.getPackageName(), 0).versionName;
        } catch (Exception e) {
            appVersion = "1.0.1";
        }

        SophixManager.getInstance().setContext(this)
                .setAppVersion(appVersion)
                .setAesKey(null)
                //.setAesKey("0123456789123456")
                .setEnableDebug(true)
                .setPatchLoadStatusStub(new PatchLoadStatusListener() {
                    @Override
                    public void onLoad(final int mode, final int code, final String info, final int handlePatchVersion) {
                        String msg = new StringBuilder("").append("Mode:").append(mode)
                                .append(" Code:").append(code)
                                .append(" Info:").append(info)
                                .append(" HandlePatchVersion:").append(handlePatchVersion).toString();
                        if (msgDisplayListener != null) {
                            msgDisplayListener.handle(msg);
                        } else {
                            cacheMsg.append("\n").append(msg);
                        }
                    }
                }).initialize();
    }

    @Override
    public void onCreate() {
        super.onCreate();
        initHotfix();
        mContext = this;
        //初始化sharedpreferences
        SharedPreferencesUtil.initialize(mContext);
        //初始化地图控件
        SDKInitializer.initialize(getApplicationContext());
        initApp();
        //是否为测试模式  正式改为false
        initDebug(false);
        initLocation();
        Utils.init(mContext);
    }

    /**
     * 初始化位置信息
     */
    public void initLocation() {
        locationService = new LocationService(getApplicationContext());
        mLocationClient = new LocationClient(getApplicationContext());
        initOption();
        mLocationClient.registerLocationListener(myListener);
        mLocationClient.start();
        Constants.bdclient = mLocationClient;
    }


    private void initDebug(boolean b) {
        LogUtil.IS_LOG = b;
//        ToastUtils.isDebug = b;
        L.isDebug = b;
        T.isDebug = b;
    }

    private void initApp() {
        PackageManager manager = getPackageManager();
        try {
            PackageInfo info = manager.getPackageInfo(getPackageName(), 0);
            Constants.versionName = info.versionName;
            Constants.versionCode = info.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }

    private void initOption() {

        LocationClientOption option = new LocationClientOption();
        option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);
        //可选，默认高精度，设置定位模式，高精度，低功耗，仅设备

        option.setCoorType("bd09ll");
        //可选，默认gcj02，设置返回的定位结果坐标系

        int span = 1000;
        option.setScanSpan(span);
        //可选，默认0，即仅定位一次，设置发起定位请求的间隔需要大于等于1000ms才是有效的

        option.setIsNeedAddress(true);
        //可选，设置是否需要地址信息，默认不需要

        option.setOpenGps(true);
        //可选，默认false,设置是否使用gps

        option.setLocationNotify(true);
        //可选，默认false，设置是否当GPS有效时按照1S/1次频率输出GPS结果

        option.setIsNeedLocationDescribe(true);
        //可选，默认false，设置是否需要位置语义化结果，可以在BDLocation.getLocationDescribe里得到，结果类似于“在北京天安门附近”

        option.setIsNeedLocationPoiList(true);
        //可选，默认false，设置是否需要POI结果，可以在BDLocation.getPoiList里得到

        option.setIgnoreKillProcess(false);
        //可选，默认true，定位SDK内部是一个SERVICE，并放到了独立进程，设置是否在stop的时候杀死这个进程，默认不杀死

        option.setEnableSimulateGps(false);
        //可选，默认false，设置是否需要过滤GPS仿真结果，默认需要

        mLocationClient.setLocOption(option);
    }


}
